#!/usr/bin/env node

/*
 * pemtobin - convert any PEM to binary file
 *
 * Copyright (c) 2015-2016 Kenji Urushima (kenji.urushima@gmail.com)
 *
 * This software is licensed under the terms of the MIT License.
 * https://kjur.github.io/jsrsasign/license
 *
 * The above copyright and license notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * Please use '-h' option for this script usage.
 * ---------------------------------------------------------
 * DESCRIPTION
 *   This script converts any PEM to binary file
 *
 * USAGE
 *   % pemtobin aaa.pem aaa.bin
 */

var program = require('commander');
var rs = require('jsrsasign');
var rsu = require('jsrsasign-util');

program
  .version('0.0.2 (2016-Sep-11)')
  .usage('[options] <input PEM file> <output binary file>')
  .description('convert any PEM file to binary')
  .parse(process.argv);

if (program.args.length !== 2)
  throw "wrong number of arguments";

var inFile  = program.args[0];
var outFile = program.args[1];

var pem = rsu.readFile(inFile);
var hex = rs.KEYUTIL.getHexFromPEM(pem);
rsu.saveFileBinByHex(outFile, hex);

